package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.exception.Assert;
import com.atguigu.srb.core.exception.BusinessException;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserLoginRecordMapper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.result.ResponseEnum;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.UserLoginRecordService;
import com.atguigu.srb.core.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author AtLong
 * @since 2022-06-11
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserLoginRecordMapper userLoginRecordMapper;

    @Override
    public String getMobileByBindCode(String bindCode) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("bind_code",bindCode);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);
        String mobile = userInfo.getMobile();
        log.debug("查询手机号准备发送消息--->>>"+mobile);
        return mobile;
    }

    @Transactional
    @Override
    public void register(RegisterVO registerVO) {
        // 判断用户是否已经注册,先查询操作
        try {
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("mobile", registerVO.getMobile());
            Integer count = baseMapper.selectCount(queryWrapper);

            Assert.isTrue(count == 0, ResponseEnum.MOBILE_EXIST_ERROR);
        } catch (BusinessException e) {
            e.printStackTrace();
        }

        // 插入用户的基本信息的更替
        UserInfo userInfo = new UserInfo();
        userInfo.setName(registerVO.getMobile());
        userInfo.setNickName(registerVO.getMobile());
        userInfo.setMobile(registerVO.getMobile());
        userInfo.setBindStatus(UserInfo.STATUS_LOCKED);
        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        userInfo.setUserType(registerVO.getUserType());

        // 设置头像的图片
        userInfo.setHeadImg("https://srb-file.oss-cn-beijing.aliyuncs.com/avatar/07.jpg");
        // 调用数据插入方法
        baseMapper.insert(userInfo);

        // 创建会员账户
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());

        // 调用新增方法
        userAccountMapper.insert(userAccount);
    }

    @Override
    public UserInfoVO login(LoginVO loginVO, String ip) {
        // 获取VO封装的属性信息
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Integer userType = loginVO.getUserType();
        // 查询获取会员信息
        QueryWrapper<UserInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.eq("mobile",mobile).eq("user_type",userType);
        UserInfo userInfo = baseMapper.selectOne(infoQueryWrapper);
         // 用户不存在的校验
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        // 校验密码
        Assert.equals(MD5.encrypt(password),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        // 用户是否被禁用
        Assert.equals(userType,userInfo.getUserType(),ResponseEnum.LOGIN_LOKED_ERROR);

        // 记录登录日志
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setCreateTime(userInfo.getCreateTime());
        userLoginRecord.setUpdateTime(userInfo.getUpdateTime());
        userLoginRecord.setIp(ip);
        userLoginRecord.setDeleted(userInfo.getDeleted());
        userLoginRecord.setUserId(userInfo.getId());
        // 调用插入方法
        userLoginRecordMapper.insert(userLoginRecord);

        // 生成token
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        System.out.println("token = " + token);

        UserInfoVO uif = new UserInfoVO();
        uif.setToken(token);
        uif.setMobile(userInfo.getName());
        uif.setName(userInfo.getName());
        uif.setNickName(userInfo.getNickName());
        uif.setPassword(userInfo.getPassword());
        uif.setUserType(userInfo.getUserType());

        // 返回结果
        return uif;
    }
}
